﻿Public Class AddArticle
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lbl_postdate.Text = Today.Date()
    End Sub

    Protected Sub btn_submit_article_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit_article.Click
        Dim Uid As Integer
        Dim Uname As String = Page.User.Identity.Name
        Uid = CInt(userID(Uname))
        Dim sql As String = " INSERT INTO Articles (A_Name, A_Desc, A_PostDate, A_UserID) VALUES (@v1, @v2, @v3, v4)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@v1", txt_Tittle.Text)
        cmd.Parameters.AddWithValue("@v2", txt_description.Text)
        cmd.Parameters.AddWithValue("@v3", lbl_postdate.Text)
        cmd.Parameters.AddWithValue("@v4", Uid)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Write("<script type='text/javascript'>alert('Successfully posted your article');</script>")
        Response.Redirect("Articles.aspx")
    End Sub
    Protected Function userID(ByVal uname As String) As Integer
        Dim uID As Integer
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnString").ConnectionString)
        conn.Open()
        Dim IDsql As String = " select aspnet_Users.UserId from aspnet_Users Where aspnet_Users.UserName = '" & uname & "'"
        Dim IDcmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(IDsql, conn)
        Dim Dreader As OleDb.OleDbDataReader
        Try
            Dreader = IDcmd.ExecuteReader()
            Dreader.Read()
            uID = Dreader("UserId")
        Catch
        End Try
        Return uID
        conn.Close()
        IDcmd.Dispose()
        conn.Dispose()
    End Function
End Class